package com.aifeng.base

import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.CompoundButton
import android.widget.RadioGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.aifeng.common_ui.event.IBaseViewEventObserver
import com.aifeng.common_ui.helper.ActivityStack
import com.aifeng.utils.cancelLoadingDialog
import com.aifeng.utils.showLoadingDialog
import net.idik.lib.slimadapter.SlimAdapter
import net.idik.lib.slimadapter.SlimAdapterEx

open class BaseFragment : Fragment(),
    IBaseViewEventObserver,
    TextWatcher,
    RadioGroup.OnCheckedChangeListener,
    CompoundButton.OnCheckedChangeListener {

    /**
     * RecyclerView数据管理的LayoutManager
     */
    protected lateinit var linearLayoutManager: LinearLayoutManager
    protected lateinit var gridLayoutManager: GridLayoutManager
    protected lateinit var staggeredGridLayoutManager: StaggeredGridLayoutManager
    /**
     * SlimAdapter的adapter
     */
    protected lateinit var mAdapter: SlimAdapter
    /**
     * SlimAdapter可加头部或底部的adapter
     */
    protected lateinit var mAdapterEx: SlimAdapterEx
    /**
     * 分页加载页数
     */
    protected var pageNum = 1
    /**
     * 分页总页数
     */
    protected var pageTotal = 1
    /**
     * 是否正在上拉加载中
     */
    protected var isLoadingMore: Boolean = false
    /**
     * 列表选择时的位置标记，可做其他字段使用
     */
    protected var mPosition: Int = 0

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //注册监听ViewModel的生命周期
        initViewModelEvent()
        getViewModel()?.let { lifecycle.addObserver(it) }
        if (getViewModelList().isNotEmpty()) {
            getViewModelList().forEach { lifecycle.addObserver(it) }
        }

        initTitle()
    }

    //网络数据请求方法
    @JvmOverloads
    protected open fun getData(
        index: Int = 0,
        isLoading: Boolean = false
    ) {}

    //初始化控件
    protected open fun initTitle() {}

    override fun getIContext(): Context = this.context!!

    override fun getLifecycleOwner(): LifecycleOwner = this

    override fun showLoading(msg: String) {
        if (msg.isEmpty()) this.activity?.showLoadingDialog()
        else this.activity?.showLoadingDialog(msg)
    }

    override fun dismissLoading() {
        this.activity?.cancelLoadingDialog()
    }

    override fun finishView() {
        this.activity?.apply {
            ActivityStack.screenManager.popActivities(this::class.java)
        }
    }

    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}

    override fun afterTextChanged(s: Editable) {}

    override fun onCheckedChanged(buttonView: CompoundButton, isChecked: Boolean) {}

    override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {}

}
